﻿using AutoMapper;
using MediatR;
using SupplyChain.Domain.Entities;
using SupplyChain.Infrastructure.Interface;
using SupplyChain.Write.API.Application.Command.Supplier;

namespace SupplyChain.Write.API.Application.CommandHandler.Supplier
{
    public class UpdSupplierProfileHandler : IRequestHandler<UpdSupplierProfileCommand, bool>
    {
        private readonly IBaseRepository<SupplierProfile> repository;
        private readonly IMapper mapper;

        public UpdSupplierProfileHandler(IBaseRepository<SupplierProfile> repository, IMapper mapper)
        {
            this.repository = repository;
            this.mapper = mapper;
        }

        public async Task<bool> Handle(UpdSupplierProfileCommand request, CancellationToken cancellationToken)
        {
            SupplierProfile profile = mapper.Map<SupplierProfile>(request);
            if (profile.Id != 0)
            {
                bool result = await repository.UpdateAsync(profile);
                return await Task.FromResult(result);
            }
            return await Task.FromResult(false);
        }
    }
}
